import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class BillDAO {
    // 添加账单
    public void addBill(String date, double amount, String category, String type, String note) throws SQLException {
        String query = "INSERT INTO bills (date, amount, category, type, note) VALUES (?, ?, ?, ?, ?)";
        try (Connection connection = DatabaseConfig.getConnection();
             PreparedStatement statement = connection.prepareStatement(query)) {
            statement.setDate(1, Date.valueOf(date));
            statement.setDouble(2, amount);
            statement.setString(3, category);
            statement.setString(4, type);
            statement.setString(5, note);
            statement.executeUpdate();
        }
    }

    // 获取所有账单
    public List<Bill> getAllBills() throws SQLException {
        String query = "SELECT * FROM bills";
        List<Bill> bills = new ArrayList<>();
        try (Connection connection = DatabaseConfig.getConnection();
             Statement statement = connection.createStatement();
             ResultSet resultSet = statement.executeQuery(query)) {
            while (resultSet.next()) {
                bills.add(new Bill(resultSet.getInt("id"), resultSet.getDate("date").toString(), resultSet.getDouble("amount"), resultSet.getString("category"), resultSet.getString("type"), resultSet.getString("note")));
            }
        }
        return bills;
    }

    // 查询指定日期的账单
    public List<Bill> queryBills(String date) throws SQLException {
        String query = "SELECT * FROM bills WHERE date = ?";
        List<Bill> bills = new ArrayList<>();
        try (Connection connection = DatabaseConfig.getConnection();
             PreparedStatement statement = connection.prepareStatement(query)) {
            statement.setDate(1, Date.valueOf(date));
            try (ResultSet resultSet = statement.executeQuery()) {
                while (resultSet.next()) {
                    bills.add(new Bill(resultSet.getInt("id"), resultSet.getDate("date").toString(), resultSet.getDouble("amount"), resultSet.getString("category"), resultSet.getString("type"), resultSet.getString("note")));
                }
            }
        }
        return bills;
    }

    // 设置每月预算
    public void setMonthlyBudget(double amount) throws SQLException {
        String query = "UPDATE budgets SET budget_limit = ? WHERE month = ?";
        try (Connection connection = DatabaseConfig.getConnection();
             PreparedStatement statement = connection.prepareStatement(query)) {
            statement.setDouble(1, amount);
            statement.executeUpdate();
        }
    }

    //查看月度统计报告
    public List<Bill> getMonthlyReport(String month) throws SQLException {

        return List.of();
    }


    public String getMonthlyReport() {
        return null;
    }
}